<?php
/**
 * Created by PhpStorm.
 * User: lyq
 * Date: 2018/9/8
 * Time: 10:39
 */
namespace app\admin\model;


class Wallet extends Common{

    //钱包完整接口地址
    static public $api_url;
    //提交的数据
    static public $postdata;

    /**
     *获取H链资产拥有量
     *
     * @param array $mobile_arr 手机号的一维数组
     */
    static public function someUser($mobile_arr=[]){
        self::set_api_url('/commerce/getBalances');
        $param_array = [
            'mobile'=>$mobile_arr,
            'notice'=>'none'
        ];
        self::organize_params($param_array);
        return self::curl_post();
    }

    /**
     *判断单个会员
     *
     * @param string $mobile 手机号
     */
    static public function oneUser($mobile=''){
        self::set_api_url('/commerce/usertrue');
        $param_array = [
            'mobile'=>$mobile
        ];
        self::organize_params($param_array);
        return self::curl_post();
    }

    /**
     *获取会员H链资产记录
     *
     * @param string $mobile 手机号
     * @param int $num 分页一页显示几条
     * @param int $page 页码
     */
    static public function hlHistory($mobile,$num,$page){
        self::set_api_url('/commerce/getDeal');
        $param_array = [
            'mobile'=>$mobile,
            'num'=>$num,
            'page'=>$page
        ];
        self::organize_params($param_array);
        return self::curl_post();
    }

    /**
     *给会员添加减H链资产
     *
     * @param string $mobile 手机号
     * @param float $coin 加减资产量
     * @param int $type 1加2减
     * @param string $remark 备注
     */
    static public function addDelCoin($mobile,$coin,$type,$remark=''){
        self::set_api_url('/commerce/setBalance');
        $param_array = [
            'code'=>base64_encode($mobile.','.$coin),
            'type'=>$type,
            'remark'=>$remark
        ];
        self::organize_params($param_array);
        return self::curl_post();
    }

    /**
     *拼接api地址
     *
     * @param string $action api地址中域名之外的具体方法名
     */
    static public function set_api_url($action){
        self::$api_url = config('post_url').$action;
    }

    /**
     *拼接参数
     *
     *@param array $param_array 参数数组
     */
    static public function organize_params($param_array){
        $time = time();
        $token = md5(md5(substr($time,3)));
        $param_array['time'] = $time;
        $param_array['token'] = $token;
        self::$postdata = json_encode($param_array);
    }

    /**
     *curl模拟post 访问
     * @return array
     */
    static function curl_post(){
        $res = curl_post(self::$api_url,self::$postdata);
        return json_decode($res,true);
    }
}